package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.CheckStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.model.CommitVoteWorkReq;
import com.ygqh.baby.model.SearchVoteWorkReq;
import com.ygqh.baby.model.VoteWorkListVo;
import com.ygqh.baby.model.YgVote;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.po.YgVoteWork;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgVoteWorkService;
import com.ygqh.baby.utils.SessionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: sunshuo
 * @date: 2018/12/25 14:03
 */
@Controller
@RequestMapping("mobile/vote")
public class YgVoteController extends BaseController {

	@Autowired
	private YgVoteWorkService ygVoteWorkService;
	@Autowired
	private RedisDao redisDao;

	/**
	 * 提交作品
	 *
	 * @param commitVoteWorkReq 作品信息
	 * @return
	 */
	@RequestMapping("commitWork")
	@ResponseBody
	public JSONPObject commitWork(CommitVoteWorkReq commitVoteWorkReq) {
		YgUser currentUser = SessionUtil.getCurrentUser();
		String result = redisDao.get("commitWork-" + currentUser.getId());
		if (StringUtils.isNotBlank(result)) {
			return new JSONPObject(commitVoteWorkReq.getCallback(), Message.error("操作频繁，稍后重试", null));
		}
		// 5秒操作延迟
		redisDao.set("commitWork-" + currentUser.getId(), "1", 5 * 1000);
		Message validateParamMsg = commitVoteWorkReq.validateParam();
		if (!Message.isSuccess(validateParamMsg)) {
			redisDao.del("commitWork-" + currentUser.getId());
			return new JSONPObject(commitVoteWorkReq.getCallback(), validateParamMsg);
		}
		YgVote voteInfo = ygVoteWorkService.getVoteInfoFromRedis();
		if (voteInfo == null) {
			redisDao.del("commitWork-" + currentUser.getId());
			return new JSONPObject(commitVoteWorkReq.getCallback(), Message.error("活动已结束", null));
		}
		Message validateRegisterDateMsg = voteInfo.validateRegisterDate();
		if (!Message.isSuccess(validateRegisterDateMsg)) {
			redisDao.del("commitWork-" + currentUser.getId());
			return new JSONPObject(commitVoteWorkReq.getCallback(), validateRegisterDateMsg);
		}
		YgVoteWork voteWork = ygVoteWorkService.findByVoteBatchCodeAndUserId(voteInfo.getVoteBatchCode(), currentUser.getId(), CheckStatus.Pass, CheckStatus.Waiting);
		if (voteWork != null) {
			redisDao.del("commitWork-" + currentUser.getId());
			return new JSONPObject(commitVoteWorkReq.getCallback(), Message.error("您的作品已提交", null));
		}
		YgVoteWork record = commitVoteWorkReq.setVoteInfo(voteInfo).setCurrentUser(currentUser).build();
		int row = ygVoteWorkService.save(record);
		Message resultMsg;
		if (row > 0) {
			ygVoteWorkService.createQrCodeAsync(record.getId());
			resultMsg = Message.success(record.getId());
		} else {
			resultMsg = Message.error("数据存储异常，请稍后重试", null);
		}
		redisDao.del("commitWork-" + currentUser.getId());
		return new JSONPObject(commitVoteWorkReq.getCallback(), resultMsg);
	}

	/**
	 * 作品列表
	 *
	 * @param searchVoteWorkReq 查询条件
	 * @return
	 */
	@RequestMapping("searchVoteWork")
	@ResponseBody
	public JSONPObject searchVoteWork(SearchVoteWorkReq searchVoteWorkReq) {
		YgVote voteInfo = ygVoteWorkService.getVoteInfoFromRedis();
		String voteBatchCode = null;
		if (voteInfo != null) {
			voteBatchCode = voteInfo.getVoteBatchCode();
		}
		QueryInfo queryInfo = new QueryInfo(searchVoteWorkReq.getStart(), searchVoteWorkReq.getLimit());
		List<VoteWorkListVo> list = ygVoteWorkService.findList(queryInfo, searchVoteWorkReq.getBabyName(), voteBatchCode, searchVoteWorkReq.getCheckStatus());
		return new JSONPObject(searchVoteWorkReq.getCallback(), Message.success(list));
	}

	/**
	 * 为自己拉票
	 *
	 * @param callback
	 * @return
	 */
	@RequestMapping("pullTicket")
	@ResponseBody
	public JSONPObject pullTicket(String callback) {
		YgVote voteInfo = ygVoteWorkService.getVoteInfoFromRedis();
		if (voteInfo == null) {
			return new JSONPObject(callback, Message.error("活动已结束", null));
		}
		Message validateVoteDateMsg = voteInfo.validateVoteDate();
		if (!Message.isSuccess(validateVoteDateMsg)) {
			return new JSONPObject(callback, validateVoteDateMsg);
		}
		YgUser currentUser = SessionUtil.getCurrentUser();
		YgVoteWork voteWork = ygVoteWorkService.findByVoteBatchCodeAndUserId(voteInfo.getVoteBatchCode(), currentUser.getId());
		if (voteWork == null) {
			return new JSONPObject(callback, Message.error("亲，您还没有报名，快去报名吧。", null));
		}
		if (CheckStatus.Reject.equals(voteWork.getCheckStatus())) {
			return new JSONPObject(callback, Message.error("亲，您的作品未通过，快去重新报名吧。", null));
		}
		if (CheckStatus.Waiting.equals(voteWork.getCheckStatus())) {
			return new JSONPObject(callback, Message.error("亲，您的作品正在审核中，请耐心等待。", null));
		}
		return new JSONPObject(callback, Message.success(voteWork.getId()));
	}

	/**
	 * 作品详情
	 *
	 * @param id       id
	 * @param callback callback
	 * @return
	 */
	@RequestMapping("detail")
	@ResponseBody
	public JSONPObject detail(@RequestParam Long id, String callback) {
		YgVoteWork voteWork = ygVoteWorkService.findById(id);
		return new JSONPObject(callback, Message.success(voteWork));
	}

	/**
	 * 获取二维码地址
	 *
	 * @param id       id
	 * @param callback callback
	 * @return
	 */
	@RequestMapping("qrCodeUrl")
	@ResponseBody
	public JSONPObject qrCodeUrl(@RequestParam Long id, String callback) {
		return new JSONPObject(callback, Message.success(ygVoteWorkService.findQrCodeUrl(id)));
	}

	/**
	 * 作品排行榜
	 *
	 * @param callback
	 * @return
	 */
	@RequestMapping("voteWorkLeaderboard")
	@ResponseBody
	public JSONPObject voteWorkLeaderboard(@RequestParam(defaultValue = "4") Integer limit, String callback) {
		YgVote voteInfo = ygVoteWorkService.getVoteInfoFromRedis();
		String voteBatchCode = null;
		if (voteInfo != null) {
			voteBatchCode = voteInfo.getVoteBatchCode();
		}
		return new JSONPObject(callback, Message.success(ygVoteWorkService.findListByLeaderboard(voteBatchCode, limit)));
	}

}
